package com.picrepo.ui;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.beardedhen.androidbootstrap.BootstrapButton;
import com.picrepo.app.AppData;
import com.picrepo.task.LoginTask;
import com.picrepo.task.LoginTask.OnLoginListerner;
import com.picrepo.util.StringFilter;

public class LoginActivity extends Activity implements 
	OnClickListener, OnLoginListerner
{
	private static final String TAG = "Vtag";
	
	private BootstrapButton mButtonLogin;
	private EditText mEditAccount;
	private EditText mEditPassword;
	private String mAccount;
	private String mPassword;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		setupView();
	}
	
	private void setupView() {
		mButtonLogin = (BootstrapButton) findViewById(R.id.login_button_login);
		mEditAccount = (EditText) findViewById(R.id.edittext_account);
		mEditPassword = (EditText) findViewById(R.id.edittext_passwd);
		
		mButtonLogin.setOnClickListener(this);
		getUserInfo();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.login_button_login:
			mAccount = StringFilter.pattern(mEditAccount.getText().toString());
			mPassword = mEditPassword.getText().toString();
			
			if (mAccount.equals("") || mAccount == null || 
					mPassword.equals("") || mPassword == null) {
				Toast.makeText(getApplicationContext(), "用户名或密码不能为空", 
						Toast.LENGTH_SHORT).show();
			}
			
			/* login when the user and passwd is not null */
			if (!(mAccount.equals("") || mAccount == null) && 
					!(mPassword.equals("") || mPassword == null)) {
				LoginTask loginTask = new LoginTask();
				loginTask.execute(mAccount, mPassword);
				loginTask.setOnLoginListerner(this);
			}
			break;

		default:
			break;
		}
		
	}

	@Override
	public void onSuccess(String result) {
		int authority = AppData.AUTHORITY_ERROR;
		String uriList = null;
		
		try {
			JSONObject json = new JSONObject(result);
			authority = json.getInt("authority");
			uriList = json.getString("urilist");
		} catch (JSONException e) {
			Log.d(TAG, e.getMessage());
			e.printStackTrace();
		}
		
		switch (authority) {
		case AppData.AUTHORITY_ADMIN:
			Intent adminIntent = new Intent(this, AdminActivity.class);
			startActivity(adminIntent);
			this.finish();
			saveUserInfo();
			break;
		
		case AppData.AUTHORITY_UPLOAD:
			Intent uploadIntent = new Intent(this, UploadActivity.class);
			uploadIntent.putExtra("username", mAccount);
			startActivity(uploadIntent);
			saveUserInfo();
			this.finish();
			break;
		
		case AppData.AUTHORITY_VIEW:
			Intent viewIntent = new Intent(this, ViewPicActivity.class);
			viewIntent.putExtra("info", uriList);
			startActivity(viewIntent);
			saveUserInfo();
			this.finish();
			break;
			
		case AppData.AUTHORITY_ERROR:
		default:
			Toast.makeText(LoginActivity.this, "用户名或密码错误", 
					Toast.LENGTH_LONG).show();
			break;
		}
	}

	@Override
	public void onFailure() {
		Toast.makeText(LoginActivity.this, "服务器无响应，登录失败", Toast.LENGTH_LONG).show();
	}
	
	/**
	 * Save account and password when login successfully.
	 */
	private void saveUserInfo() {
		SharedPreferences userInfo = getSharedPreferences("user_info", MODE_PRIVATE);
		userInfo.edit().putString("account", mAccount).commit();
		userInfo.edit().putString("passwd", mPassword).commit();
	}
	
	/**
	 * Get the user information if exists.
	 */
	private void getUserInfo() {
		SharedPreferences userInfo = getSharedPreferences("user_info", MODE_PRIVATE);
		String account = userInfo.getString("account", "");
		String passwd = userInfo.getString("passwd", "");
		
		if (!account.equals("")) {
			mEditAccount.setText(account);
		}
		
		if (!passwd.equals("")) {
			mEditPassword.setText(passwd);
		}
	}
}
